home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15161 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  61 lines

  1. Path: phoenix.csc.calpoly.edu!not-for-mail
  2. From: nbonfili@phoenix.csc.calpoly.edu (Nicholas R Bonfilio)
  3. Newsgroups: comp.lang.c++
  4. Subject: Function Prototypes in C++
  5. Date: 3 Apr 1996 12:40:26 -0800
  6. Organization: California Polytechnic State University at San Luis Obispo
  7. Message-ID: <4junnq$2k1@phoenix.csc.calpoly.edu>
  8. NNTP-Posting-User: nbonfili@phoenix.csc.calpoly.edu
  9.  
  10. I know that function prototypes are required in standard C++ as opposed to C
  11. where they are not required.  But, what I don't know for certain is the rules
  12. regarding the scoping of function protos...
  13.  
  14. That is, can I declare the prototypes as "global": external to the main()
  15. routine?
  16.  
  17. Should all prototypes go inside of the main() routine?
  18.  
  19. Or should the prototypes be placed inside of each routine where the
  20. particular function is called?  (This could have been asked in the C lang
  21. discussion group, I realize.)
  22.  
  23. An example:
  24. -----------------------------------------------------------------------------
  25.  
  26. int i = 4,
  27.     j = 5,
  28.     k = 4;
  29.  
  30. main()
  31. {
  32.   real result;
  33.  
  34.   result = average(i,j,k);
  35.   cout << "Here is the answer: " << result << endl;
  36. }
  37.  
  38.  
  39. real average(int a, int b, int c)
  40. {
  41.   return (sum(a,b,c)/3.0);
  42. }
  43.  
  44.  
  45. int sum(int x, int y, int z) 
  46. {
  47.   return x+y+z; 
  48. }
  49.  
  50. -----------------------------------------------------------------------------
  51. Where would the prototypes for each function go?
  52. All in main?
  53. All external to main?
  54. or should sum's go in average and average's in main?
  55.  
  56. -- 
  57.  
  58. TTYL, Nick
  59. (nbonfili@galaxy.csc.calpoly.edu)
  60.  
  61.